package src.main.java.com.test;

public class RandowErArr {
  public static void main(String[] args) {
    // 把一个以为数组打乱成二位驻足 [0,1,2,3,4,5,6,7,8]
    int[] arr = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
    // 打乱数组
    for (int i = 0; i < arr.length; i++) {
      int index = (int) (Math.random() * arr.length);
      int temp = arr[i];
      arr[i] = arr[index];
      arr[index] = temp;
    }
    // 遍历数组
    for (int i = 0; i < arr.length; i++) {
      System.out.print(arr[i] + " ");
    }
    System.out.println();

    // 创建一个二位数组
    int[][] arr2 = new int[3][3];
    // 给二维数组添加数据
    // 方法一
    // int index = 0;
    // for (int i = 0; i < arr2.length; i++) {
    // for (int j = 0; j < arr2[i].length; j++) {
    // arr2[i][j] = (int) arr[index];
    // index++;
    // }
    // }
    // 方法二
    for (int i = 0; i < arr.length; i++) {
      arr2[i / 3][i % 3] = arr[i];
    }

    // 打印二位数组数据
    for (int i = 0; i < arr2.length; i++) {
      for (int j = 0; j < arr2[i].length; j++) {
        System.out.print(arr2[i][j] + " ");
      }
      System.out.println();
    }
  }
}
